//--------------------------------------------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "dktelly.h"
#include "dkterm.h"

//--------------------------------------------------------------------

uint8_t telly_flags;							// flags for tx/rx routines


//--------------------------------------------------------------------
void telly_init(void)
{
	telly_flags = TELLY_ECHO | TELLY_SWAPCRLF | TELLY_ADDCR;

	UBRR1 = 3; 										// 57.6k @ 3.68MHz
	UCSR1A = (0<<U2X1);
//	UCSR1B = (1<<TXEN0);							// only enable transmitter to telly for now
	UCSR1B = (1<<TXEN1) | (1<<RXEN1);

	telly_put( 0x1B );							// ESC Z = reset telly to power-on equivalent
	telly_put( 'z' );

	_delay_ms( 100 );

	telly_put( 0x1B );					
	telly_put( '~' );							// send tx-enable sequence to telly
	telly_put( '~' );
	telly_put( '~' );
	telly_put( '~' );


//	telly_put( 0x1B );							// ESC E = clear screen and home (VT52)
//	telly_put( 'E' );
} 


//--------------------------------------------------------------------
int telly_put(char c ) 
{
	while( ! (UCSR1A & (1<<UDRE1)) )
		;
	UDR1 = c;


	if( (telly_flags&TELLY_ADDCR) && c=='\n' )			// if ADDCR is on, add cr to lf
		telly_put( '\r' );								// this can get moved to dkio.c?

	return 0;
}

//--------------------------------------------------------------------
char telly_charhere( void ) 
{
	char data;


	if( UCSR1A & _BV(FE1) )
		data = UDR1;
	if( UCSR1A & _BV(DOR1) )
		data = UDR1;
	while( UCSR1A & _BV(RXC1) )				// flush serial buffer
		data = UDR1;

	telly_put( 0x1B );					
	telly_put( '|' );

	while( ! (UCSR1A & _BV(RXC1)) )
	{
		if( UCSR1A & _BV(FE1) )
		{
			data = UDR1;
			return 0;
		}
		if( UCSR1A & _BV(DOR1) )
		{
			data = UDR1;
			return 0;
		}
	}

	data = UDR1;
	return data;
}

//--------------------------------------------------------------------
char telly_charat( uint8_t row, uint8_t col ) 
{
	char data;


	if( UCSR1A & _BV(FE1) )
		data = UDR1;
	if( UCSR1A & _BV(DOR1) )
		data = UDR1;
	while( UCSR1A & _BV(RXC1) )				// flush serial buffer
		data = UDR1;

	telly_put( 0x1B );					
	telly_put( '`' );
	telly_put( 32 + row-1 );					
	telly_put( 32 + col-1 );					


	while( ! (UCSR1A & _BV(RXC1)) )
	{
		if( UCSR1A & _BV(FE1) )
		{
			data = UDR1;
			return 0;
		}
		if( UCSR1A & _BV(DOR1) )
		{
			data = UDR1;
			return 0;
		}
	}

	data = UDR1;

	return data;
}

//--------------------------------------------------------------------
void telly_goto( uint8_t row, uint8_t col ) 
{
	telly_put( 0x1B );					
	telly_put( 'Y' );
	telly_put( 32 + row-1 );					
	telly_put( 32 + col-1 );					

	return;
}

//--------------------------------------------------------------------
/*
ISR(USART1_RX_vect) 
{
	uint8_t data;


	if( UCSR1A & _BV(FE1) )
	{
		data = UDR1;
		return;
	}
	if( UCSR1A & _BV(DOR1) )
	{
		data = UDR1;
		return;	
	}

	data = UDR1;
//	rx_buffer[ rx_wr_ptr ] = data;

//	if( data == '\x03' )
//		io_ctrl_char = data;						// ctrl-c has been received


//	rx_wr_ptr++;
//	rx_wr_ptr %= 128;
}
*/

//--------------------------------------------------------------------

